//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;

using CommunityServer.Components;
using CommunityServer.Files.Components;

namespace CommunityServer.Files.Controls 
{

	[
	ParseChildren(true)
	]
	public class JumpDropDownList : DropDownList 
	{
		CSContext csContext = CSContext.Current;

		public JumpDropDownList() 
		{

			// Set up some default property values
			//
			AutoPostBack = true;
			SelectedIndexChanged += new System.EventHandler(Location_Changed);

			// head of the drop down
			//
			Items.Add(new ListItem( FileGalleryResourceManager.GetString("Navigation_JumpDropDownList_Title"), "" ));
			Items.Add(new ListItem( FileGalleryResourceManager.GetString("Navigation_JumpDropDownList_Seperator") ));

			// standard "home" links
			//
			Items.Add(new ListItem( FileGalleryResourceManager.GetString("Navigation_JumpDropDownList_Files_Home"), FileGalleryUrls.Instance().Home));

			// User Options to display, based if the user is signed in or not.
			//
			if (csContext.User.IsFileAdministrator || csContext.User.IsModerator) 
			{
				Items.Add(new ListItem( FileGalleryResourceManager.GetString("Navigation_JumpDropDownList_Seperator") ));
				Items.Add(new ListItem( FileGalleryResourceManager.GetString("Navigation_JumpDropDownList_AdminOptions") ));
				if ( csContext.User.IsFileAdministrator  )
					Items.Add(new ListItem( FileGalleryResourceManager.GetString("Navigation_JumpDropDownList_Indent1") + FileGalleryResourceManager.GetString("Navigation_JumpDropDownList_AdminHome"), Globals.GetSiteUrls().ControlPanel ));
				Items.Add(new ListItem( FileGalleryResourceManager.GetString("Navigation_JumpDropDownList_Indent1") + FileGalleryResourceManager.GetString("Navigation_JumpDropDownList_ModeratorHome"), Globals.GetSiteUrls().ModerationHome ));
			}            

			// seperator
			//
			Items.Add(new ListItem( FileGalleryResourceManager.GetString("Navigation_JumpDropDownList_Seperator") ));

			// User Options to display, based if the user is signed in or not.
			//
			if (!csContext.User.IsAnonymous) 
			{
				Items.Add(new ListItem( FileGalleryResourceManager.GetString("Navigation_JumpDropDownList_UserOptions") ));
				Items.Add(new ListItem( ResourceManager.GetString("Navigation_JumpDropDownList_Indent1") + ResourceManager.GetString("Navigation_JumpDropDownList_Profile"), Globals.GetSiteUrls().UserEditProfile ));
			} 
			else 
			{
				Items.Add(new ListItem( FileGalleryResourceManager.GetString("Navigation_JumpDropDownList_UserOptions") ));
				Items.Add(new ListItem( FileGalleryResourceManager.GetString("Navigation_JumpDropDownList_Indent1") + FileGalleryResourceManager.GetString("Navigation_JumpDropDownList_Login"), Globals.GetSiteUrls().Login ));
				Items.Add(new ListItem( FileGalleryResourceManager.GetString("Navigation_JumpDropDownList_Indent1") + FileGalleryResourceManager.GetString("Navigation_JumpDropDownList_CreateAccount"), Globals.GetSiteUrls().UserRegister ));
				Items.Add(new ListItem( FileGalleryResourceManager.GetString("Navigation_JumpDropDownList_Indent1") + FileGalleryResourceManager.GetString("Navigation_JumpDropDownList_ForgotPassword"), Globals.GetSiteUrls().UserForgotPassword ));
			}

			// seperator
			//
			Items.Add(new ListItem( FileGalleryResourceManager.GetString("Navigation_JumpDropDownList_Seperator") ));

			// Get all forum groups and the remaining items are displayed
			// as ForumGroups and Forums.
			//
			ArrayList folderGroups = Folders.GetFolderGroups(true, false, false);
			foreach (Group group in folderGroups) 
			{
				// Add the folder group
				//
				Items.Add(new ListItem(group.Name, "g-" + group.GroupID));

				if (!group.HasSections)
					group.Sections = Folders.GetFoldersByGroupID(group.GroupID, false, true, false);

				// Add all folder recursively.
				//
				RecursiveAddFolders (0, group.Sections);

				// Add the folder group
				//
				Items.Add(new ListItem( FileGalleryResourceManager.GetString("Navigation_JumpDropDownList_Seperator") ));
			}
		}

		void RecursiveAddFolders (int depth, ArrayList folders) 
		{
            
			foreach (Folder folder in folders) 
			{
				// We only go 3 deep
				//

				switch (depth) 
				{
					case 0:
						Items.Add(new ListItem(FileGalleryResourceManager.GetString("Navigation_JumpDropDownList_Indent1") + folder.Name, "f-" + folder.ApplicationKey));
						if (folder.Sections.Count > 0)
							RecursiveAddFolders((depth + 1), folder.Sections);
						break;

					case 1:
						Items.Add(new ListItem(FileGalleryResourceManager.GetString("Navigation_JumpDropDownList_Indent2") + folder.Name, "f-" + folder.ApplicationKey));
						if (folder.Sections.Count > 0)
							RecursiveAddFolders((depth + 1), folder.Sections);
						break;

					case 2:
						Items.Add(new ListItem(FileGalleryResourceManager.GetString("Navigation_JumpDropDownList_Indent3") + folder.Name, "f-" + folder.ApplicationKey));
						if (folder.Sections.Count > 0)
							RecursiveAddFolders((depth + 1), folder.Sections);
						break;

					default:
						return;

				}
			}
		}


		// *********************************************************************
		//  Location_Changed
		//
		/// <summary>
		/// User wants to jump to a new location
		/// </summary>
		/// 
		// ********************************************************************/ 
		private void Location_Changed(Object sender, EventArgs e) 
		{

			DropDownList jumpLocation = (DropDownList) sender;
			string jumpValue = jumpLocation.SelectedItem.Value;

			if (jumpValue.StartsWith("/")) 
			{
				Page.Response.Redirect(jumpValue);
			} 
			else if (jumpValue.StartsWith("g")) 
			{
				int folderGroupId = 0;
				folderGroupId = Convert.ToInt32(jumpValue.Substring(jumpValue.IndexOf("-") + 1));
				Page.Response.Redirect(FileGalleryUrls.Instance().ViewFolderGroup(folderGroupId));
			} 
			else if (jumpValue.StartsWith("f")) 
			{
				string appKey = jumpValue.Substring(jumpValue.IndexOf("-") + 1);
				Page.Response.Redirect(FileGalleryUrls.Instance().ViewFolder(appKey));
			} 
			else 
			{
				Page.Response.Redirect(Globals.GetSiteUrls().Home);
			}

			// End the response
			Page.Response.End();
		}


		// *********************************************************************
		//  DisplayText
		//
		/// <summary>
		/// Text preceding the drop down list of options
		/// </summary>
		/// 
		// ********************************************************************/ 
		public virtual String DisplayText 
		{
			get 
			{
				Object state = ViewState["DisplayText"];
				if ( state != null ) 
				{
					return (String)state;
				}
				return FileGalleryResourceManager.GetString("Navigation_JumpDropDownList_displayText");
			}
			set 
			{
				ViewState["DisplayText"] = value;
			}
		}

	}
}
